from tkinter import ttk
import tkinter
import tkinter.messagebox
import pymysql
from PIL import Image,ImageTk


# 连接数据库FittingRoom
conn = pymysql.connect(
    host="localhost",
    user="root",
    passwd="******",
    database="FittingRoom"   
)
if conn:
    print("数据库连接成功")
    
#以下是顾客界面
# 查询顾客信息
def getcus():
    cursor = conn.cursor()
    tmpcid = cuid.get().strip()
    sql = ''
    if len(tmpcid) == 0:
        sql = "select * from customer"
    else:
        sql = "select * from customer where Cid like " + "'%" + tmpcid + "'"
    cursor.execute(sql)
    results = cursor.fetchall()

    for i in tree.get_children():
        tree.delete(i)

    for row in results:
        tree.insert("", "end", values=row)

    cursor.close()

# 插入顾客信息
def insertcus():
    cursor = conn.cursor()
    tmpcid = cuid.get().strip()
    tmpcname = cuname.get().strip()
    tmpcsex = cusex.get().strip()
    tmpcroom = curoom.get().strip()

    cursor.execute("INSERT INTO Customer VALUES (%s,%s,%s,%s)", (tmpcid,tmpcname, tmpcsex, tmpcroom))
    conn.commit()
    tkinter.messagebox.showinfo('提示', message='信息插入成功!')
    cursor.close()

# 更新顾客信息
def updatecus():
    cursor = conn.cursor()
    tmpcid = cuid.get().strip()
    tmpcname = cuname.get().strip()
    tmpcsex = cusex.get().strip()
    tmpcroom = curoom.get().strip()

    cursor.execute("UPDATE Customer SET cname=%s, csex=%s, room=%s WHERE cid=%s", (tmpcname, tmpcsex, tmpcroom, tmpcid))
    conn.commit()
    tkinter.messagebox.showinfo('提示', message='信息更新成功!')
    
    cursor.close()

# 删除顾客信息
def deletecus():
    tmpcid = cuid.get().strip()
    cursor = conn.cursor()
    cursor.execute("delete from Customer where Cid = %s", (tmpcid,))
    conn.commit()
    tkinter.messagebox.showinfo('提示', message='信息删除成功!')
    cursor.close()

#Customer表窗口
def Window_customer():
    window = tkinter.Toplevel(root)
    window.title('customer')
    window.geometry('460x360')

    L1 = tkinter.Label(window, text="顾客编号:")
    L1.place(x=30, y=30, width=70, anchor="nw")

    global cuid
    cuid = tkinter.StringVar()
    E1 = tkinter.Entry(window, bd=1, textvariable=cuid)
    E1.place(x=110, y=30, width=80, anchor="nw")

    L2 = tkinter.Label(window, text="顾客姓名:")
    L2.place(x=30, y=70, width=70, anchor="nw")

    global cuname
    cuname =tkinter. StringVar()
    E2 = tkinter.Entry(window, bd=1, textvariable=cuname)
    E2.place(x=110, y=70, width=80, anchor="nw")

    L3 = tkinter.Label(window, text="顾客性别:")
    L3.place(x=220, y=30, width=70, anchor="nw")

    global cusex
    cusex = tkinter.StringVar()
    E3 = tkinter.Entry(window, bd=1, textvariable=cusex)
    E3.place(x=300, y=30, width=70, anchor="nw")

    L4 = tkinter.Label(window, text="试衣间编号:")
    L4.place(x=210, y=70, width=80, anchor="nw")

    global curoom
    curoom = tkinter.StringVar()
    E4 = tkinter.Entry(window, bd=1, textvariable=curoom)
    E4.place(x=300, y=70, width=80, anchor="nw")

    Bth = tkinter.Button(window, text="查询", command=getcus)
    Bth.place(x=20, y=120, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="添加", command=insertcus)
    Bth.place(x=130, y=120, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="更新", command=updatecus)
    Bth.place(x=240, y=120, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="清除", command=deletecus)
    Bth.place(x=350, y=120, width=90, anchor="nw")

    # Treeview
    global tree
    tree = ttk.Treeview(window, show='headings', columns=('cid', 'cname', 'csex', 'room'), selectmode=tkinter.BROWSE)
    tree.column('cid', anchor='center', width=90)
    tree.column('cname', anchor='center', width=90)
    tree.column('csex', anchor='center', width=80)
    tree.column('room', anchor='center', width=80)
    tree.heading('cid', text='顾客编号')
    tree.heading('cname', text='顾客姓名')
    tree.heading('csex', text='顾客性别')
    tree.heading('room', text='试衣间编号')
    tree.place(x=20, y=160, width=420, height=180, anchor='nw')

    window.mainloop()
    

# 服装界面
# 查询服装信息
def getclo():
    cursor = conn.cursor()
    tmpsku = sku.get().strip()
    sql = ''
    if len(tmpsku) == 0:
        sql = "select * from Clothes"
    else:
        sql = "select * from Clothes where SKU like " + "'%" + tmpsku + "'"
    cursor.execute(sql)
    results = cursor.fetchall()

    for i in treeclo.get_children():
        treeclo.delete(i)

    for row in results:
        treeclo.insert("", "end", values=row)

    cursor.close()


# 插入服装信息
def insertclo():
    cursor = conn.cursor()
    tmpsku = sku.get().strip()
    tmpstylename = stylename.get().strip()
    tmpcolor = color.get().strip()
    tmpprice = price.get().strip()
    tmpstock = stock.get().strip()

    cursor.execute("INSERT INTO Clothes VALUES (%s,%s,%s,%s,%s)", (tmpsku, tmpstylename, tmpcolor, tmpprice, tmpstock))
    conn.commit()
    tkinter.messagebox.showinfo('提示', message='信息插入成功!')
    
    cursor.close()


# 更新服装信息
def updateclo():
    cursor = conn.cursor()
    tmpsku = sku.get().strip()
    tmpstylename = stylename.get().strip()
    tmpcolor = color.get().strip()
    tmpprice = price.get().strip()
    tmpstock = stock.get().strip()

    cursor.execute("UPDATE Clothes SET stylename=%s, color=%s, price=%s, stock=%s WHERE sku=%s",
                   (tmpstylename, tmpcolor, tmpprice, tmpstock, tmpsku))
    conn.commit()
    tkinter.messagebox.showinfo('提示', message='信息更新成功!')
    
    cursor.close()


# 删除服装信息
def deleteclo():
    tmpsku = sku.get().strip()
    cursor = conn.cursor()
    cursor.execute("delete from Clothes where sku = %s", (tmpsku,))
    conn.commit()
    (tkinter.messagebox.showinfo('提示', message='信息删除成功!'))
    cursor.close()


# 服装界面
def Window_clothes():
    window = tkinter.Toplevel(root)
    window.title('服装信息(Clothes)')
    window.geometry('460x360')

    L1 = tkinter.Label(window, text="SKU:")
    L1.place(x=30, y=20, width=70, anchor="nw")

    global sku
    sku = tkinter.StringVar()
    E1 = tkinter.Entry(window, bd=1, textvariable=sku)
    E1.place(x=110, y=20, width=100, anchor="nw")

    L2 = tkinter.Label(window, text="款名:")
    L2.place(x=30, y=60, width=70, anchor="nw")

    global stylename
    stylename = tkinter.StringVar()
    E2 = tkinter.Entry(window, bd=1, textvariable=stylename)
    E2.place(x=110, y=60, width=100, anchor="nw")

    L3 = tkinter.Label(window, text="颜色:")
    L3.place(x=30, y=100, width=70, anchor="nw")

    global color
    color = tkinter.StringVar()
    E3 = tkinter.Entry(window, bd=1, textvariable=color)
    E3.place(x=110, y=100, width=100, anchor="nw")

    L4 = tkinter.Label(window, text="价格:")
    L4.place(x=210, y=20, width=80, anchor="nw")

    global price
    price = tkinter.StringVar()
    E4 = tkinter.Entry(window, bd=1, textvariable=price)
    E4.place(x=300, y=20, width=100, anchor="nw")

    L5 = tkinter.Label(window, text="库存:")
    L5.place(x=210, y=60, width=80, anchor="nw")

    global stock
    stock = tkinter.StringVar()
    E5 = tkinter.Entry(window, bd=1, textvariable=stock)
    E5.place(x=300, y=60, width=100, anchor="nw")

    Bth = tkinter.Button(window, text="查询", command=getclo)
    Bth.place(x=20, y=130, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="添加", command=insertclo)
    Bth.place(x=130, y=130, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="更新", command=updateclo)
    Bth.place(x=240, y=130, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="清除", command=deleteclo)
    Bth.place(x=350, y=130, width=90, anchor="nw")

    # Treeview
    global treeclo
    treeclo = ttk.Treeview(window, show='headings', columns=('sku', 'stylename', 'color', 'price', 'stock'),
                        selectmode=tkinter.BROWSE)
    treeclo.column('sku', anchor='center', width=90)
    treeclo.column('stylename', anchor='center', width=90)
    treeclo.column('color', anchor='center', width=80)
    treeclo.column('price', anchor='center', width=80)
    treeclo.column('stock', anchor='center', width=80)
    treeclo.heading('sku', text='SKU')
    treeclo.heading('stylename', text='款名')
    treeclo.heading('color', text='颜色')
    treeclo.heading('price', text='价格')
    treeclo.heading('stock', text='库存')
    treeclo.place(x=20, y=160, width=420, height=180, anchor='nw')

    window.mainloop()

#以下是fitting
# 查询试穿信息
def getfit():
    cursor = conn.cursor()
    tmpcid = cuid.get().strip()
    tmpsku = cusku.get().strip()
    sql = ''
    if len(tmpcid) == 0:
        sql = "select * from Fitting"
    else:
        sql = "select * from Fitting where Cid like '%" + tmpcid + "%' and sku like '%" + tmpsku + "%'"

    cursor.execute(sql)
    results = cursor.fetchall()

    for i in treefit.get_children():
        treefit.delete(i)

    for row in results:
        treefit.insert("", "end", values=row)

    cursor.close()


# 插入试穿信息
def insertfit():
    cursor = conn.cursor()
    tmpcid = cuid.get().strip()
    tmpsku = cusku.get().strip()
    tmptimes = cutimes.get().strip()

    cursor.execute("Select stock from Clothes where sku = %s", (tmpsku))
    q = cursor.fetchone()

    if q and int(tmptimes) <= q[0]:
        cursor.execute("INSERT INTO Fitting VALUES (%s,%s,%s)", (tmpcid, tmpsku, tmptimes))
        cursor.execute("Update Clothes Set Stock=stock-%s where SKU=%s", (tmptimes, tmpsku))
        conn.commit()
        tkinter.messagebox.showinfo('提示', message='信息插入成功!')
    else:
        tkinter.messagebox.showerror('提示', "库存不足")

    cursor.close()


# 更新试穿信息
def updatefit():
    cursor = conn.cursor()
    tmpcid = cuid.get().strip()
    tmpsku = cusku.get().strip()
    tmptimes = cutimes.get().strip()

    cursor.execute("Select stock from Clothes where sku = %s", (tmpsku))
    q = cursor.fetchone()

    cursor.execute("Select num from Fitting where Cid = %s and SKU = %s", (tmpcid, tmpsku))
    p = cursor.fetchone()

    if q and p and int(tmptimes) - p[0] <= q[0]:
        cursor.execute("UPDATE Fitting SET cid=%s, sku=%s, num=%s WHERE cid=%s", (tmpcid, tmpsku, tmptimes, tmpcid))
        cursor.execute("UPDATE Clothes SET stock=stock-(%s-%s) WHERE SKU = %s", (tmptimes, p[0], tmpsku))
        conn.commit()
        tkinter.messagebox.showinfo('提示', message='信息更新成功!')
    else:
        tkinter.messagebox.showerror('提示', "库存不足")

    cursor.close()


# 删除试穿信息
def deletefit():
    tmpcid = cuid.get().strip()
    tmpsku = cusku.get().strip()
    cursor = conn.cursor()
    cursor.execute("SET @tmp1 = 0")
    cursor.execute("SELECT num into @tmp1 from Fitting where Cid = %s and SKU = %s", (tmpcid, tmpsku))
    cursor.execute("delete from Fitting where Cid = %s and sku = %s", (tmpcid, tmpsku))
    cursor.execute("UPDATE Clothes SET stock=stock+@tmp1 WHERE SKU = %s", (tmpsku))
    conn.commit()
    tkinter.messagebox.showinfo('提示', message='信息删除成功!')
    cursor.close()


#Fitting表窗口
def Window_Fitting():
    window = tkinter.Toplevel(root)
    window.title('试衣信息(Fitting)')
    window.geometry('500x360')

    L1 = tkinter.Label(window, text="顾客编号:")
    L1.place(x=30, y=30, width=70, anchor="nw")

    global cuid
    cuid = tkinter.StringVar()
    E1 = tkinter.Entry(window, bd=1, textvariable=cuid)
    E1.place(x=110, y=30, width=80, anchor="nw")

    L2 = tkinter.Label(window, text="SKU:")
    L2.place(x=30, y=70, width=70, anchor="nw")

    global cusku
    cusku = tkinter.StringVar()
    E2 = tkinter.Entry(window, bd=1, textvariable=cusku)
    E2.place(x=110, y=70, width=80, anchor="nw")

    L3 = tkinter.Label(window, text="试穿件数:")
    L3.place(x=220, y=30, width=70, anchor="nw")

    global cutimes
    cutimes = tkinter.StringVar()
    E3 = tkinter.Entry(window, bd=1, textvariable=cutimes)
    E3.place(x=300, y=30, width=70, anchor="nw")

    Bth = tkinter.Button(window, text="查询", command=getfit)
    Bth.place(x=20, y=120, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="添加", command=insertfit)
    Bth.place(x=130, y=120, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="更新", command=updatefit)
    Bth.place(x=240, y=120, width=90, anchor="nw")

    Bth = tkinter.Button(window, text="清除", command=deletefit)
    Bth.place(x=350, y=120, width=90, anchor="nw")

    # Treeview
    global treefit
    treefit = ttk.Treeview(window, show='headings', columns=('cid', 'sku', 'times'), selectmode=tkinter.BROWSE)
    treefit.column('cid', anchor='center', width=90)
    treefit.column('sku', anchor='center', width=90)
    treefit.column('times', anchor='center', width=80)

    treefit.heading('cid', text='顾客编号')
    treefit.heading('sku', text='SKU')
    treefit.heading('times', text='试穿件数')

    treefit.place(x=20, y=160, width=420, height=180, anchor='nw')

    window.mainloop()

#退出窗口
def Window_exit():
    # 创建root的子窗口
    window = tkinter.Toplevel()
    window.title("退出管理系统")
    window.geometry('400x300')
    Button1_Yes = tkinter.Button(window, text='确认', bg='silver', font=('Arial', 12), command=lambda: root.destroy(),
                            width=10)
    Button1_Yes.place(x=50, y=200, anchor='nw')
    # 关于"取消"组件，此处绑定函数destroy()用于关闭窗口
    Button2_No = tkinter.Button(window, text='取消', bg='silver', font=('Arial', 12), command=lambda: window.destroy(),
                           width=10)
    Button2_No.place(x=250, y=200, anchor='nw')
    # 在界面中显示文本框,打印result的信息
    result = tkinter.StringVar()
    result.set(">>>您确认离开系统吗？<<<")
    Show_result = tkinter.Label(window, bg="white", fg="blue", font=("宋体", 15), bd='0', anchor='center', textvariable=result)
    Show_result.place(x="50", y="75", width="300", height="50")

    window.mainloop()


#汇总界面
def Window_summary():
    window = tkinter.Toplevel(root)
    window.title('汇总(Summary)')
    window.geometry('460x200')
    
    L1 = tkinter.Label(window, text="今日总客流量")
    L1.place(x=40, y=70, width=100,anchor="nw")
    
    L2 = tkinter.Label(window, text='已登记试衣人数')
    L2.place(x=180, y=70, width=100,anchor="nw")
    
    L3 = tkinter.Label(window, text='已登记试衣件数')
    L3.place(x=320, y=70, width=100,anchor="nw")
    
    # 创建变量用于存储统计结果
    total_customers = tkinter.StringVar()
    total_fittings = tkinter.StringVar()
    total_items = tkinter.StringVar()
    
    # 创建标签显示统计结果
    L1_result = tkinter.Label(window, textvariable=total_customers)
    L1_result.place(x=70, y=100, width=30,anchor="nw")
    
    L2_result = tkinter.Label(window, textvariable=total_fittings)
    L2_result.place(x=210, y=100, width=30,anchor="nw")
    
    L3_result = tkinter.Label(window, textvariable=total_items)
    L3_result.place(x=350, y=100, width=30,anchor="nw")
    
    # 查询按钮的回调函数
    def query_summary():
        cursor = conn.cursor()
        # 这里需要根据你的数据库设计编写相应的查询语句
        cursor.execute("SELECT COUNT(*) FROM Customer")
        total_customers.set(cursor.fetchone()[0])
        
        cursor.execute("SELECT COUNT(DISTINCT cid) FROM Fitting")
        total_fittings.set(cursor.fetchone()[0])
        
        cursor.execute("SELECT SUM(num) FROM Fitting")
        total_items.set(cursor.fetchone()[0])
        
        cursor.close()
    
    Bth = tkinter.Button(window, text='查询', command=query_summary)
    Bth.place(x=190, y=20, width=80, anchor='nw')
    
    window.mainloop()


#主函数
if __name__ == '__main__':
    #创建主窗口
    root = tkinter.Tk()
    root.title("试衣间管理系统")
    root.geometry('500x350')

    Button1_customer = tkinter.Button(root, text = '顾客信息(Customer)',bg = 'silver', font = ('Arial', 12), command = Window_customer, width = 15)
    Button1_customer.place(x = 30, y = 50, anchor = 'nw')

    Button2_clothes = tkinter.Button(root, text = '服装信息(Clothes)', bg = 'silver', font = ('Arial', 12), command = Window_clothes, width = 15)
    Button2_clothes.place(x = 30, y = 100, anchor = 'nw')

    Button3_clothes = tkinter.Button(root, text='试穿信息(Fitting)', bg='silver', font=('Arial', 12), command=Window_Fitting, width=15)
    Button3_clothes.place(x=30, y=150, anchor='nw')

    Button4_Exit = tkinter.Button(root, text = '汇总(Summary)', bg = 'silver', font = ('Arial', 12), command = Window_summary, width = 15)
    Button4_Exit.place(x = 30, y = 200, anchor = 'nw')

    Button5_Exit = tkinter.Button(root, text = '退出试衣间系统', bg = 'silver', font = ('Arial', 12), command = Window_exit, width = 15)
    Button5_Exit.place(x = 30, y = 250, anchor = 'nw')

    result = tkinter.StringVar()
    result.set(">>>欢迎使用试衣间管理系统<<<\n   Edition:  V1.1   \n   @Author: djy,ygg,hy,wt\n Complete Time:  2024/5/10")
    Show_result = tkinter.Label(root, bg="white", fg="black", font=("微雅软黑", 16), bd='0', anchor='nw', textvariable=result)
    Show_result.place(x="210", y="200", width="250", height="90")

    # 加载图片
    img = Image.open("/Users/daijiayi/Desktop/WechatIMG9110.jpg")  # 确保图片路径正确，并且是支持的格式
    img = img.resize((230, 140), Image.LANCZOS)  
    imgTk = ImageTk.PhotoImage(img)

    # 创建一个标签来显示图片
    label = tkinter.Label(root, image=imgTk)
    label.image = imgTk  # 保持对图像的引用
    label.place(x=220, y=40)  # 设置标签的位置
    
    root.mainloop()
    
